package com.cgah

import com.cgah.exceptions.CustomerNotFoundException


class CustomerController {

    def customerService;

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        [customerInstanceList: customerService.list(params), customerInstanceTotal: customerService.countOfCustomers()]// customerService.list(params)
    }

    def create = {
        // S-1: Create a Grails service method that supports creating a new customer (integration test)
        return [customerInstance: customerService.create(params)];
    }

    def save = {
        def customerInstance = new Customer(params)
        if (customerService.save(customerInstance)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])}"
            redirect(action: "show", id: customerInstance.id)
        }
        else {
            render(view: "create", model: [customerInstance: customerInstance])
        }
    }

    def show = {
        try {
            [customerInstance: customerService.getCustomerById(params.id)]
        } catch (CustomerNotFoundException cnfe) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])}"
            redirect(action: "list")
        }
    }

    def edit = {
        try {
            [customerInstance: customerService.getCustomerById(params.id)] //
        } catch (CustomerNotFoundException cnfe) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])}"
            redirect(action: "list")
        }
    }


    def update = {

        try {
            def customerInstance = customerService.updateCustomerWithParams(params);
            if (!customerInstance.hasErrors()) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])}"
                redirect(action: "show", id: customerInstance.id)
            } else {
                render(view: "edit", model: [customerInstance: customerInstance])
                //return
            }
        } catch (CustomerNotFoundException cnfe) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])}"
            redirect(action: "list")
        }
    }


    def delete = {
        try {
            if (customerService.deleteCustomerById(params.id)) {
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])}"
                redirect(action: "list")
            }
            else {
                flash.message = "Delete Failed.  You cannot delete a customer with an associated listing or bid."
                redirect(action: "show", id: params.id)
            }
        }
        catch (org.springframework.dao.DataIntegrityViolationException e) {
            flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])}"
            redirect(action: "show", id: params.id)
        }
        catch (CustomerNotFoundException cnfe) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])}"
            redirect(action: "list")

        }
    }
}
